from email.encoders import encode_noop
import sys,os,time
from datetime import datetime
import json
from xml.dom.minidom import Element
import numpy as np

configFileName = "config.json"
configFileDir = os.path.abspath(os.getcwd())
configFilePath  = os.path.join(configFileDir, configFileName)

class ConfigFile:
    config = {
        "frameType": {
            "1": "byte100",
            "2": "byte160",
            "3": "byte160_2"
        },
        "byte100": {
            "describe": "100byte/250ms",
            "short_len": 16,
            "short_cnt": 50,
            "element_dict": {
                "short1": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s1_变量1",
                        "bit_start": 0,
                        "bit_len": 16,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short2": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s2_变量1",
                        "bit_start": 0,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s2_变量2",
                        "bit_start": 8,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short3": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s3_变量1",
                        "bit_start": 0,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s3_变量2",
                        "bit_start": 4,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s3_变量3",
                        "bit_start": 8,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short4": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s4_变量1",
                        "bit_start": 0,
                        "bit_len": 2,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s4_变量2",
                        "bit_start": 2,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s4_变量3",
                        "bit_start": 3,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short5": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s1_变量1",
                        "bit_start": 0,
                        "bit_len": 16,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short6": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s6_变量1",
                        "bit_start": 0,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s6_变量2",
                        "bit_start": 8,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short7": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s7_变量1",
                        "bit_start": 0,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s7_变量2",
                        "bit_start": 4,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s3_变量3",
                        "bit_start": 8,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short8": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s4_变量1",
                        "bit_start": 0,
                        "bit_len": 2,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s4_变量2",
                        "bit_start": 2,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s4_变量3",
                        "bit_start": 3,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short9": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s1_变量1",
                        "bit_start": 0,
                        "bit_len": 16,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short10": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s2_变量1",
                        "bit_start": 0,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s2_变量2",
                        "bit_start": 8,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short11": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s3_变量1",
                        "bit_start": 0,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s3_变量2",
                        "bit_start": 4,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s3_变量3",
                        "bit_start": 8,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short12": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s4_变量1",
                        "bit_start": 0,
                        "bit_len": 2,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s4_变量2",
                        "bit_start": 2,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s4_变量3",
                        "bit_start": 3,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short13": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s1_变量1",
                        "bit_start": 0,
                        "bit_len": 16,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short14": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s2_变量1",
                        "bit_start": 0,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s2_变量2",
                        "bit_start": 8,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short15": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s3_变量1",
                        "bit_start": 0,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s3_变量2",
                        "bit_start": 4,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s3_变量3",
                        "bit_start": 8,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short16": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s4_变量1",
                        "bit_start": 0,
                        "bit_len": 2,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s4_变量2",
                        "bit_start": 2,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s4_变量3",
                        "bit_start": 3,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short17": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s1_变量1",
                        "bit_start": 0,
                        "bit_len": 16,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short18": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s2_变量1",
                        "bit_start": 0,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s2_变量2",
                        "bit_start": 8,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short19": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s3_变量1",
                        "bit_start": 0,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s3_变量2",
                        "bit_start": 4,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s3_变量3",
                        "bit_start": 8,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short20": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s4_变量1",
                        "bit_start": 0,
                        "bit_len": 2,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s4_变量2",
                        "bit_start": 2,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s4_变量3",
                        "bit_start": 3,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short21": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s1_变量1",
                        "bit_start": 0,
                        "bit_len": 16,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short22": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s2_变量1",
                        "bit_start": 0,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s2_变量2",
                        "bit_start": 8,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short23": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s3_变量1",
                        "bit_start": 0,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s3_变量2",
                        "bit_start": 4,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s3_变量3",
                        "bit_start": 8,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short24": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s4_变量1",
                        "bit_start": 0,
                        "bit_len": 2,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s4_变量2",
                        "bit_start": 2,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s4_变量3",
                        "bit_start": 3,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short25": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s1_变量1",
                        "bit_start": 0,
                        "bit_len": 16,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short26": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s2_变量1",
                        "bit_start": 0,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s2_变量2",
                        "bit_start": 8,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short27": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s3_变量1",
                        "bit_start": 0,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s3_变量2",
                        "bit_start": 4,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s3_变量3",
                        "bit_start": 8,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short28": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s4_变量1",
                        "bit_start": 0,
                        "bit_len": 2,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s4_变量2",
                        "bit_start": 2,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s4_变量3",
                        "bit_start": 3,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short29": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s1_变量1",
                        "bit_start": 0,
                        "bit_len": 16,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short30": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s2_变量1",
                        "bit_start": 0,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s2_变量2",
                        "bit_start": 8,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short31": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s3_变量1",
                        "bit_start": 0,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s3_变量2",
                        "bit_start": 4,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s3_变量3",
                        "bit_start": 8,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short32": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s4_变量1",
                        "bit_start": 0,
                        "bit_len": 2,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s4_变量2",
                        "bit_start": 2,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s4_变量3",
                        "bit_start": 3,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short33": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s1_变量1",
                        "bit_start": 0,
                        "bit_len": 16,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short34": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s2_变量1",
                        "bit_start": 0,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s2_变量2",
                        "bit_start": 8,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short35": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s3_变量1",
                        "bit_start": 0,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s3_变量2",
                        "bit_start": 4,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s3_变量3",
                        "bit_start": 8,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short36": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s4_变量1",
                        "bit_start": 0,
                        "bit_len": 2,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s4_变量2",
                        "bit_start": 2,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s4_变量3",
                        "bit_start": 3,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short37": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s1_变量1",
                        "bit_start": 0,
                        "bit_len": 16,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short38": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s2_变量1",
                        "bit_start": 0,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s2_变量2",
                        "bit_start": 8,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short39": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s3_变量1",
                        "bit_start": 0,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s3_变量2",
                        "bit_start": 4,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s3_变量3",
                        "bit_start": 8,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short40": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s4_变量1",
                        "bit_start": 0,
                        "bit_len": 2,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s4_变量2",
                        "bit_start": 2,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s4_变量3",
                        "bit_start": 3,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short41": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s3_变量1",
                        "bit_start": 0,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s3_变量2",
                        "bit_start": 4,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s3_变量3",
                        "bit_start": 8,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short42": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s4_变量1",
                        "bit_start": 0,
                        "bit_len": 2,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s4_变量2",
                        "bit_start": 2,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s4_变量3",
                        "bit_start": 3,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short43": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s1_变量1",
                        "bit_start": 0,
                        "bit_len": 16,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short44": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s2_变量1",
                        "bit_start": 0,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s2_变量2",
                        "bit_start": 8,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short45": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s3_变量1",
                        "bit_start": 0,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s3_变量2",
                        "bit_start": 4,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s3_变量3",
                        "bit_start": 8,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short46": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s4_变量1",
                        "bit_start": 0,
                        "bit_len": 2,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s4_变量2",
                        "bit_start": 2,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s4_变量3",
                        "bit_start": 3,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short47": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s1_变量1",
                        "bit_start": 0,
                        "bit_len": 16,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short48": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s2_变量1",
                        "bit_start": 0,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s2_变量2",
                        "bit_start": 8,
                        "bit_len": 8,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short49": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s3_变量1",
                        "bit_start": 0,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s3_变量2",
                        "bit_start": 4,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s3_变量3",
                        "bit_start": 8,
                        "bit_len": 4,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                },
                "short50": {
                    "var1": {
                        "var_ID": 1,
                        "name": "s4_变量1",
                        "bit_start": 0,
                        "bit_len": 2,
                        "unit": "rpm",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var2": {
                        "var_ID": 2,
                        "name": "s4_变量2",
                        "bit_start": 2,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    },
                    "var3": {
                        "var_ID": 3,
                        "name": "s4_变量3",
                        "bit_start": 3,
                        "bit_len": 1,
                        "unit": "bit",
                        "sign": "10",
                        "isDispGraph": "false",
                        "isDispBar": "false",
                        "isDispSquare": "false"
                    }
                }
            }
        }
    }    
    
    def __init__(self):
        super().__init__()

        self.dataBuffer = b''
        self.dataOneFrame = b''
        self.frameEleList = []
        self.create_mill = 0

    # 创建bin和csv文件
    def createFile(self):
        folderDir = time.strftime(".\\%Y\\%m%d\\",time.localtime())
        folder = os.path.exists(folderDir)
        if not folder:
            os.makedirs(folderDir)                                          # 创建文件夹存放数据文件

        localTime = time.strftime("%Y%m%d%H%M%S",time.localtime())
        originaFileName = folderDir + localTime + ".bin"
        csvFileName = folderDir + localTime + ".csv"
        self.binFile = open(originaFileName, 'ab')                     # 创建保存原始数据的二进制文件
        self.csvFile = open(csvFileName, 'a+')                              # 创建保存分割后数据的CSV文件

        now = datetime.now()
        self.create_mill = now.hour * 3600000 + now.minute * 60000 + now.second * 1000 + round(now.microsecond/1000)    

    def closeFile(self):
        self.binFile.close()
        self.csvFile.close()
    # 解析一帧数据
    def parseOneFrameData(self,bytes):
        self.getOneFramData(bytes)
        self.splitOneFrameData()
        return self.frameEleList

    # 从缓存中找到一帧数据
    def getOneFramData(self,bytes):
        start = 0
        if (len(self.dataBuffer) >= 40*1024*1024):      # 最大缓存40MB的数据
            self.dataBuffer = b''
        self.dataBuffer += bytes   
        data_len = len(self.dataBuffer)
        print("===========================================data_len:",data_len)
        if(data_len < 425):
            return
        while True:
            # print("-------------:",self.dataBuffer[start+17*24])
            if self.dataBuffer[start] ==  0 \
                and self.dataBuffer[start+17*9] ==  9 \
                and self.dataBuffer[start+17*17] ==  17 \
                and self.dataBuffer[start+17*20] ==  20 \
                and self.dataBuffer[start+17*24] ==  24:
                self.dataOneFrame = b''
                for i in range(25):
                    self.dataOneFrame += self.dataBuffer[start+17*i+1:start+17*i+5]     # 得到一帧数据
                print("dataOneFrame = ",self.dataOneFrame)
                self.dataBuffer = self.dataBuffer[start+425:]                           # 清除已经得到的数据
                break
            else:
                start += 1
                if ((start+425) > len(self.dataBuffer)):
                    break
                else:
                    continue

    # 将一帧数据根据配置文件分割成一帧文本数据
    def splitOneFrameData(self):
        # 1.根据帧类型得到分割用的配置文件
        # 2.根据配置文件分割帧内容，到列表中，保留帧内容去除其他，  发送到文本显示界面中
        # 3.添加时间戳保存到csv文件中   self.dataSaveToFile() 
        if self.dataOneFrame == b'':
            return
        intIndex = 0
        type = ConfigFile.config["frameType"][1]
        for short_i in list(ConfigFile.config[type]["element_dict"].keys()):
            tempdata = self.dataOneFrame[intIndex*2:(intIndex+1)*2]
            int_temp = int().from_bytes(tempdata,byteorder='big')       # 得到一个short数据(2字节)
            intIndex += 1
            #print("int_temp = ",'%#x'%int_temp)                         # 16进制方式打印获得的一个short
            for var_j in list(ConfigFile.config[type]["element_dict"][short_i].keys()):     # 得到一个short的所有变量
                # print("short_i = " + str(short_i) + "  var_j = " + str(var_j))  
                bitVar = self.split_short(int_temp,
                                        ConfigFile.config["byte100"]["element_dict"][short_i][var_j]["bit_start"],
                                        ConfigFile.config["byte100"]["element_dict"][short_i][var_j]["bit_len"]) 
                #print("bit = ",'%#x'%bitVar)
                self.frameEleList.append(bitVar)
        self.dataOneFrame = b''

        if not self.frameEleList:
            return
        #now = datetime.now()
        #now_mill = now.hour * 3600000 + now.minute * 60000 + now.second * 1000 + round(now.microsecond/1000)
        #now_mill = now_mill - self.create_mill
        now_mill = round(time.time(), 3) 
        print("now_mill:==============:",now_mill)
        self.frameEleList.insert(0,now_mill)


    def split_short(self,var,start,len):
        '''
            1.通过start和len分割一个int(4字节)类型的变量
            2.start开始位,len变量位数,end = start + len
            3.例b'\x12\x34',start=8,len=4得到2
            4.添加越界判断：
                start、len、end大于16的处理
        '''
        end = start + len
        var = var << (16-end)         
        var = var & 0xFFFF
        var = var >> (16-end+start)   
        return var  
    
    # 保存一帧数据到csv文件
    def dataSaveToCSVFile(self):
        print("frameEleList",self.frameEleList)
        if not self.frameEleList:
            return
        for var in self.frameEleList:                               # 写入数据
            self.csvFile.write(str(var))  
            self.csvFile.write(',')
        self.frameEleList = []
        self.csvFile.write('\n')                                    # 写入换行符
        self.csvFile.flush()                                        # 刷新缓冲区,将数据立刻写入到文件 

    def save(self,path):
        pass

    # 
    def loadFile(self,path):
        if not os.path.exists(path):
            # 如果文件不存在就创建config.json,将文件中的config写入文件
            with open(path,'w',encoding="utf-8") as f:
                json.dump(self.config,f,indent=4,ensure_ascii=False)
            return  
             
        with open(path,encoding="utf-8") as f:
            cfg = json.load(f)   # 加载json格式文件，返回python对象
        self.config = cfg
        return

    def split_int(var,start,end):
        '''
            1.通过start和end分割一个int(4字节)类型的变量
            2.start开始位,end结束位(不包含)
            3.例b'\x12\x34\x56\x78',start=16,end=20得到4
        '''
        var = var << (32-end)         #16
        var = var & 0xFFFFFFFF
        var = var >> (32-end+start)   #8
        return var  

    def getFrameVar(self,frameData):
        '''
            1.接收一帧数据，根据配置文件进行分割得到各个变量值
        '''
        zz = 0
        for int_i in list(self.config["byte160"]["element_dict"].keys()):
            int_temp = int().from_bytes(frameData[zz*4:(zz+1)*4],byteorder='big')
            zz += 1
            for var_j in list(self.config["byte160"]["element_dict"][int_i].keys()):
                # 拆分int类型数据int_temp
                bitVar = self.split_int(int_temp,
                                    self.config["byte160"]["element_dict"][int_i][var_j]["bit_start"],
                                    self.config["byte160"]["element_dict"][int_i][var_j]["bit_end"]) 
                print("bit = ",'%#x'%bitVar,";",bitVar)

    def splitArray160(self,array,list):
        pass
        #for i in range(0,self.config["byte160"]["int1"]["var_num"]):
            #var1 = array[]
            #list.append()

    def __getitem__(self, idx):
        return self.config[idx]
        

    def __setitem__(self, idx, v):
        self.config[idx] = v

    def __str__(self) -> str:
        return json.dumps(self.config)



if __name__ == '__main__':
    config = ConfigFile()
    config.loadFile(configFilePath) 
    print("配置文件路径：",configFilePath) 
    for short_i in list(config["byte100"]["element_dict"].keys()):
        for var_j in list(config["byte100"]["element_dict"][short_i].keys()):
            #lineChart:折线图，barChart:条形图，squareChart:方波图
            config["byte100"]["element_dict"][short_i][var_j]["isDispLine"] = "true"  
            config["byte100"]["element_dict"][short_i][var_j]["graphStyle"] = "lineChart" 
            # del config["byte100"]["element_dict"][short_i][var_j]["isDispGraph"]
            # del config["byte100"]["element_dict"][short_i][var_j]["isDispBar"]
            # del config["byte100"]["element_dict"][short_i][var_j]["isDispSquare"]

    #print(json.dumps(config.config, indent=4, ensure_ascii=False)) 
    with open(configFilePath,'w',encoding="utf-8") as f:
        json.dump(config.config,f,indent=4,ensure_ascii=False)  
